package com.zlm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlm.constants.SysConstants;
import com.zlm.domain.ResponseResult;
import com.zlm.domain.entity.LoginUser;
import com.zlm.domain.vo.CommentVo;
import com.zlm.domain.vo.PageVo;
import com.zlm.enums.AppHttpCodeEnum;
import com.zlm.exception.SystemException;
import com.zlm.mapper.CommentMapper;
import com.zlm.domain.entity.Comment;
import com.zlm.service.CommentService;
import com.zlm.service.UserService;
import com.zlm.utils.BeanCopyUtils;
import com.zlm.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2024-12-10 17:03:42
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;
    @Override
    public ResponseResult commentList(String commentType,Long articleId, Integer pageNum, Integer pageSize) {
        if(Objects.isNull(pageNum)){
            pageNum = SysConstants.ARTICLE_PAGE_NUM;
        }
        if(Objects.isNull(pageSize)){
            pageSize = SysConstants.ARTICLE_PAGE_SIZE;
        }

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysConstants.ARTICLE_COMMENT_TYPE.equals(commentType),Comment::getArticleId,articleId);
        queryWrapper.eq(Comment::getRootId,SysConstants.ARTICLE_COMMENT_ROOT_ID);
        queryWrapper.eq(Comment::getType,commentType);
        queryWrapper.orderByDesc(Comment::getCreateTime);
        Page<Comment> page = new Page<>(pageNum, pageSize);
        Page<Comment> commentPage = page(page, queryWrapper);

        List<Comment> commentList = commentPage.getRecords();

        List<CommentVo> commentVos = toCommentVoList(commentList);

        //查询子评论
        commentVos.forEach(commentVo -> {
            List<CommentVo> children = getChildren(commentVo.getId());
            commentVo.setChildren(children);
        });

        return ResponseResult.okResult(new PageVo(commentVos,commentPage.getTotal()));
    }

    @Override
    public ResponseResult addComment(Comment comment) {
        if (!StringUtils.hasText(comment.getContent())) {
            throw new SystemException(AppHttpCodeEnum.COMMENT_NOT_EXIST);
        }
        save(comment);
        return null;
    }

    /**
     * 查询子评论
     * @param id 根评论id
     * @return
     */
    private List<CommentVo> getChildren(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> comments = list(queryWrapper);
        return toCommentVoList(comments);
    }

    /**
     * 转换评论列表为评论vo列表
     * @param list 评论列表
     * @return
     */
    private List<CommentVo> toCommentVoList(List<Comment> list){
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        //遍历vo
        for (CommentVo commentVo : commentVos) {
            //通过createBy查询评论人昵称并赋值
            String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
            commentVo.setUsername(nickName);
            //通过toCommentUserId查询评论人昵称并赋值
            if(commentVo.getToCommentUserId()!=-1){
                String toCommentNickName = userService.getById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentUserName(toCommentNickName);
            }
        }

        return commentVos;
    }
}

